package com.six.core.controller;

import com.six.core.entity.Emp;
import com.six.core.entity.TbUser;
import com.six.core.mapper.UserMapper;
import com.six.core.service.UserService;
import com.six.core.util.JwtUtil;
import com.six.core.util.WebUtil;
import com.six.core.vo.RegVo;
import com.six.core.vo.Result;
import com.six.core.vo.TbUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * @Description:
 * @author:LiWei
 * @date:2023年02月28日9:45
 */
@Api(value = "/user/user", tags = {":"})
@RestController
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
//    @Autowired
//    private UserMapper userMapper;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "currentPage", value = "", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "pageSize", value = "", required = true),
            @ApiImplicitParam(paramType = "body", dataType = "TbUserVo", name = "tbUserVo", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "")
    @RequestMapping("list/{currentPage}/{pageSize}")
    public Result list(@PathVariable Integer currentPage, @PathVariable Integer pageSize,  @RequestBody TbUserVo tbUserVo){
        Result all = userService.findAll(currentPage, pageSize, tbUserVo);
        return new Result(200,"查询成功",all);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "userId", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "")
    @RequestMapping("findByUserId")
    public Result findByUserId(){
        return userService.findByUserId();
    }


    @RequestMapping("updateUserPassword")
    public Result updateUserPassword(@RequestBody RegVo regVo){
        return userService.updateUserPassword(regVo.getMobile(),regVo.getRepassword());

    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "TbUser", name = "tbUser", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "")
    @RequestMapping("addUser")
    public Result addUser(@RequestBody RegVo regVo){
        return userService.addUser(regVo);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "TbUser", name = "tbUser", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "")
    @RequestMapping("updateUser")
    public Result updateUser(@RequestBody TbUser tbUser){
        return userService.updateUser(tbUser);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "")
    @RequestMapping("deleteById")
    public Result deleteById(Integer id){
        return userService.deleteById(id);
    }


    @ApiOperation(value = "", notes = "", httpMethod = "GET")
    @GetMapping("loginOut")
    public Result loginOut(){
        String token = WebUtil.getRequest().getHeader("token");
        redisTemplate.delete(token);
        return new Result(200,"退出成功");
    }
    
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "userid", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/setUserIsType")
    public int setUserIsType(Integer userid){
        return userService.setUserIsType(userid);
    }


    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("findByUserNum")
    public Integer findByUserNum(){
        return userService.findByUserNum();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "mobile", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/findByMobile")
    public TbUser findByMobile(String mobile){
        return userService.findByMobile(mobile);
    }

    @PostMapping("/send/{phone}")
    public Result send(@PathVariable String phone){
       return userService.send(phone);
    }
}
